<?php
/**
 *
 * Application for the function: Update Sale
 *
 * This page allows a salesperson to update sale information.
 *
 *
 * @author Chi Zhang
 * @version 1.0
 * @package dummy-auto
 *
 */

include('../config.inc');
require('dummy-app.inc');

function emp_type($empid) {
        $sql = mysql_query("
                select  Type
                  from  Employee
                 where  EmpID = $empid;
               ");

        if ($sql) {
                $row = mysql_fetch_array($sql);
                return $row[0];
        } else
                exit_404(mysql_error());
}

session_start();

if ( ! ( session_id() && isset($_SESSION['empid'])
         && emp_type($_SESSION['empid']) == 'S')   )
{
        $tree = make_app("Update Sale :: Invalid User!");
        $app = $tree->app;
        $app->addChild('p', "Only Authorized Salesperson (Employee) Can Use This Function.");

        if (! ( session_id() && isset($_SESSION['empid'])) )
                session_end();

} elseif ($_GET['user_create']) {

        if ($_GET['uname']) {

                if (! mysql_query("
                        insert into Customer value (
                        0, '$_GET[uname]', '$_GET[addr]', '$_GET[phone]',
                        '$_GET[email]');
                        ") ) exit_404(mysql_error());

                if (! ($cus_num = mysql_insert_id()) ) exit_404(mysql_error());
                if (! mysql_query("commit;")) exit_404(mysql_error());
                    
        } else exit_404('You must fill customer\'s name!');

        $tree = make_app("Update Sale :: Add Customer Info");
        $app = $tree->app;
        $app->addChild('h2', "Customer information have been added successfully!");

        $app->addChild('input');
        $app->input['type'] = 'button';
        $app->input['value'] = 'Continue';
        $app->input['onclick'] = "load_app('salesperson', {
                vin : '$_GET[vin]', plan : $_GET[plan], unum : $cus_num,
                date : '$_GET[date]', price : $_GET[price], update : 'continue'
        })";

} elseif ($_GET['update']) {

        if (!($_GET['vin'] && $_GET['plan'] && $_GET['date'] && $_GET['price']))
        {
                if ($_GET['update'] == 'continue') {
                        if ($_GET['unum'])
                                exit_404("Transmission error! Please start
 again with the customer number: $_GET[unum].");
                        else
                                exit_404('Transmission error! Please check the database.');
                } else exit_404('All field (except customer num) need to be entered');
        }

        if (! $_GET['unum']) {

                $tree = make_app("Update Sale :: Creating Customer Info");
                $app = $tree->app;
                $app->h2 = "Enter the Information of the Customer:";
                $app->form['action'] = 'app/salesperson.php';
                $form = $app->form;

                $form->label[] = 'Full Name: ';
                add_child($form, 'input',
                          array('name' => 'uname', 'type' => 'text')
                        );

                $form->label[] = 'Address: ';
                add_child($form, 'input', 
                          array('name' => 'addr', 'type' => 'text')
                        );

                $form->label[] = 'Phone Number: ';
                add_child($form, 'input', 
                          array('name' => 'phone', 'type' => 'text')
                        );

                $form->label[] = 'Email: ';
                add_child($form, 'input', 
                          array('name' => 'email', 'type' => 'text')
                        );


                add_child($form, 'input', 
                          array('name' => 'vin', 'type' => 'hidden',
                                'value' => "$_GET[vin]")
                        );
                add_child($form, 'input', 
                          array('name' => 'plan', 'type' => 'hidden',
                                'value' => "$_GET[plan]")
                        );
                add_child($form, 'input', 
                          array('name' => 'date', 'type' => 'hidden',
                                'value' => "$_GET[date]")
                        );
                add_child($form, 'input', 
                          array('name' => 'price', 'type' => 'hidden',
                                'value' => "$_GET[price]")
                        );

                $form->addChild('p')->addChild('input');
                $form->p->input['type'] = 'submit';
                $form->p->input['name'] = 'user_create';
                $form->p->input['value'] = 'submit';

                $form->p->addChild('input');
                $form->p->input[1]['type'] = 'reset';
                $form->p->input[1]['value'] = 'reset';

                echo $tree->asXML();
                exit;
        }


        if (! mysql_query("
                insert into Sale value (
                0, $_SESSION[empid], '$_GET[vin]', '$_GET[plan]',
                $_GET[unum], '$_GET[date]', '$_GET[price]');
                    ") ) exit_404(mysql_error());

        if (! mysql_query("commit;")) exit_404(mysql_error());

        $tree = make_app("Update Maintenance :: Update Successful");
        $app = $tree->app;
        $app->addChild('h2', "Update maintenance have been updated successfully!");

        $app->addChild('input');
        $app->input['type'] = 'button';
        $app->input['value'] = 'Finish';
        $app->input['onclick'] = "load_app('salesperson')";

} else {

        $tree = make_app("Update Sale");
        $app = $tree->app;
        $app->addChild('h2', "Update a Sale Record:");

        $app->addChild('form');
        $app->form['action'] = 'app/salesperson.php';
        $app->form->addChild('p', "Please enter the following information:");

        $app->form->addChild('table')->addChild('tr');
        $app->form->table->tr->addChild('td', "Car VIN: ");
        $app->form->table->tr->addChild('td')->addChild('input');
        $app->form->table->tr->td[1]->input['type'] = "text";
        $app->form->table->tr->td[1]->input['name'] = "vin";
        $app->form->table->tr->td[1]->input['maxlength'] = "8";

        $app->form->table->addChild('tr');
        $app->form->table->tr[1]->addChild('td', "Feature Plan: ");
        $app->form->table->tr[1]->addChild('td')->addChild('input');
        $app->form->table->tr[1]->td[1]->input['type'] = "text";
        $app->form->table->tr[1]->td[1]->input['name'] = "plan";

        $app->form->table->addChild('tr');
        $app->form->table->tr[2]->addChild('td', "Customer Num: ");
        $app->form->table->tr[2]->addChild('td')->addChild('input');
        $app->form->table->tr[2]->td[1]->input['type'] = "text";
        $app->form->table->tr[2]->td[1]->input['name'] = "unum";
        $app->form->table->tr[2]->td[1]->input['maxlength'] = "7";

        $app->form->table->addChild('tr')->addChild(
                'td', "* leave it empty if it is a new customer."
                );
        $app->form->table->tr[3]->td['colspan'] = "2";

        $app->form->table->addChild('tr');
        $app->form->table->tr[4]->addChild('td', "Date: (yyyy-mm-dd)");
        $app->form->table->tr[4]->addChild('td')->addChild('input');
        $app->form->table->tr[4]->td[1]->input['type'] = "text";
        $app->form->table->tr[4]->td[1]->input['name'] = "date";
        $app->form->table->tr[4]->td[1]->input['maxlength'] = "10";

        $app->form->table->addChild('tr');
        $app->form->table->tr[5]->addChild('td', "Selling Price: ");
        $app->form->table->tr[5]->addChild('td')->addChild('input');
        $app->form->table->tr[5]->td[1]->input['type'] = "text";
        $app->form->table->tr[5]->td[1]->input['name'] = "price";


        $app->form->addChild('p')->addChild('input');
        $app->form->p[1]->input['type'] = "submit";
        $app->form->p[1]->input['name'] = "update";
        $app->form->p[1]->input['value'] = "submit";

        $app->form->p[1]->addChild('input');
        $app->form->p[1]->input[1]['type'] = "reset";
        $app->form->p[1]->input[1]['value'] = "reset";
}

echo $tree->asXML();
?>
